package com.bugbuff.dubbo.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.bugbuff.common.pojo.PageResult;
import com.bugbuff.common.pojo.UserInfo;
import com.bugbuff.dubbo.server.api.QueryInfoListApi;
import com.bugbuff.dubbo.server.pojo.Comment;
import com.bugbuff.dubbo.server.vo.CommentInfoVo;
import com.bugbuff.dubbo.server.vo.CommentVo;
import com.bugbuff.dubbo.server.vo.PageInfo;
import com.bugbuff.dubbo.server.vo.CommentInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class CommentInfoService {
    @Reference
    private QueryInfoListApi queryInfoListApi;

    @Autowired
    private  UserInfoService userInfoService;


    public PageResult queryCommentList(Integer pagesize, Integer page, String sortProp, String sortOrder, String publishId) {


        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);



        PageInfo<Comment> pageInfo = queryInfoListApi.queryCommentList(pagesize, page, publishId);
        //得到评论集合
        List<Comment> records = pageInfo.getRecords();

        pageResult.setCounts(this.queryInfoListApi.userCommentCount(publishId));
        int pages = PageUtil.totalPage(this.queryInfoListApi.userCommentCount(publishId), pagesize);
        pageResult.setPages(pages);

        List<CommentInfoVo> list =new ArrayList<>();
        //查询用户信息
        List<Object> userIds = CollUtil.getFieldValues(records, "userId");
        if(CollUtil.isEmpty(userIds)){
            return pageResult;
        }
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByUserIdList(userIds);

        for (Comment comment : records) {
            CommentInfoVo commentInfoVo =new CommentInfoVo();
            commentInfoVo.setId(comment.getPublishId().toHexString());
            commentInfoVo.setUserId(comment.getUserId());
            commentInfoVo.setContent(comment.getContent());
            commentInfoVo.setCreateDate(comment.getCreated());

            for (UserInfo userInfo : userInfoList) {
                if (ObjectUtil.equals(commentInfoVo.getUserId(), userInfo.getUserId())) {
                    commentInfoVo.setNickname(userInfo.getNickName());
                    break;
                }
            }
            list.add(commentInfoVo);
        }

        // 排序
        if (StringUtils.isNotEmpty(sortProp)) {
            this.compareList(list, sortProp, sortOrder);
        }else {
            this.compareList(list,"created","desc");
        }

        // 给编号
//        for (int i=1;i<=list.size();i++ ){
//            CommentInfoVo videoInfoVo = list.get(i - 1);
//            videoInfoVo.setId(""+i);
//        }
        pageResult.setItems(list);
        return pageResult;




    }



    public void compareList(List<CommentInfoVo> list, String sortProp, String sortOrder) {

        switch (sortProp) {
            case "created":
                if (sortOrder.equals("ascending"))
                    Collections.sort(list, new Comparator<CommentInfoVo>() {
                        @Override
                        public int compare(CommentInfoVo o1, CommentInfoVo o2) {
                            return o1.getCreateDate().compareTo(o2.getCreateDate());
                        }
                    });
                else {
                    Collections.sort(list, new Comparator<CommentInfoVo>() {
                        @Override
                        public int compare(CommentInfoVo o1, CommentInfoVo o2) {
                            return o2.getCreateDate().compareTo(o1.getCreateDate());
                        }
                    });
                }
                return;
//            case "like":
//                if (sortOrder.equals("ascending"))
//                    Collections.sort(list, new Comparator<CommentInfoVo>() {
//                        @Override
//                        public int compare(CommentInfoVo o1, CommentInfoVo o2) {
//                            return o1.getLikeCount().compareTo(o2.getLikeCount());
//                        }
//                    });
//                else {
//                    Collections.sort(list, new Comparator<CommentInfoVo>() {
//                        @Override
//                        public int compare(CommentInfoVo o1, CommentInfoVo o2) {
//                            return o2.getLikeCount().compareTo(o1.getLikeCount());
//                        }
//                    });
//                }
//            case "comment":
//                if (sortOrder.equals("ascending"))
//                    Collections.sort(list, new Comparator<CommentInfoVo>() {
//                        @Override
//                        public int compare(CommentInfoVo o1, CommentInfoVo o2) {
//                            return o1.getCommentCount().compareTo(o2.getCommentCount());
//                        }
//                    });
//                else {
//                    Collections.sort(list, new Comparator<CommentInfoVo>() {
//                        @Override
//                        public int compare(CommentInfoVo o1, CommentInfoVo o2) {
//                            return o2.getCommentCount().compareTo(o1.getCommentCount());
//                        }
//                    });
//                }
        }

    }
}
